package com.shine.icis.action;

import java.util.Calendar;
import java.util.Collection;

import com.shine.cotrun.sql.QueryParameters;
import com.shine.cotrun.web.comm.ListPage;
import com.shine.esim.struts.webutil.SessionParam;
import com.shine.icis.blo.IAccrualInfoService;
import com.shine.icis.blo.ISubmitConfService;
import com.shine.icis.config.SysCodeDictLoader;
import com.shine.icis.config.SysConstants;
import com.shine.icis.hbean.AccrualInfo;
import com.shine.icis.exception.ICISException;
import com.shine.icis.hbean.SubmitConf;
import com.shine.icis.util.EFRameInf;

/**
 * 基础信息管理->证券公司营业部信息文件
 * @author zsy
 *
 */
public class AccrualInfoAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7638121893867812633L;
	
	private IAccrualInfoService accrualInfoService;
	private ISubmitConfService submitConfService;
	//input param
	private int pageNo;
	private int pageSize;
	private long id;
	
	private String tradeDate;//交易日期
	private String arapType;//应收应付类型
	private String oppPar;//对方机构
	private String mktCodeSipf;//市场代码
	private String arapBusiType;//业务类型
	private String curTypeSipf;//币种类别
	private String arapReason;//应收付原因
	private double arapAmt;//应收应付金额
	private String harapDate;//已收付日期
	private String arapDate;//应收付日期
	private String summary;//摘要信息
	private String busiDate;//业务日期
	
	//output param
	private String currentTradeDate;//当前交易日
	private String nextTradeDate;//下一个交易日
	private ListPage<AccrualInfo> accrualInfoPage;
	private Collection<SubmitConf> arapTypeConfs;//A13应收应付类型
	private Collection<SubmitConf> arapReasonConfs;//A13应收付原因
	private Collection<SubmitConf> oppParConfs;//A13对方机构
	private Collection<SubmitConf> mktCodeSipfConfs;//A13市场代码
	private Collection<SubmitConf> arapBusiTypeConfs;//A13业务类型
	private Collection<SubmitConf> curTypeSipfConfs;//A13币种
	
	private String accrualManType;//应收应付管理方式
	
	public void setAccrualInfoService(IAccrualInfoService accrualInfoService) {
		this.accrualInfoService = accrualInfoService;
	}
	public void setSubmitConfService(ISubmitConfService submitConfService) {
		this.submitConfService = submitConfService;
	}
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public String getCurrentTradeDate() {
		return currentTradeDate;
	}
	public String getNextTradeDate() {
		return nextTradeDate;
	}
	public ListPage<AccrualInfo> getAccrualInfoPage() {
		return accrualInfoPage;
	}
	public Collection<SubmitConf> getArapTypeConfs() {
		return arapTypeConfs;
	}
	public Collection<SubmitConf> getArapReasonConfs() {
		return arapReasonConfs;
	}
	public Collection<SubmitConf> getOppParConfs() {
		return oppParConfs;
	}
	public Collection<SubmitConf> getMktCodeSipfConfs() {
		return mktCodeSipfConfs;
	}
	public Collection<SubmitConf> getArapBusiTypeConfs() {
		return arapBusiTypeConfs;
	}
	public Collection<SubmitConf> getCurTypeSipfConfs() {
		return curTypeSipfConfs;
	}
	public String getAccrualManType() {
		return accrualManType;
	}
	public void setId(long id) {
		this.id = id;
	}
	public void setTradeDate(String tradeDate) {
		this.tradeDate = tradeDate;
	}
	public void setArapType(String arapType) {
		this.arapType = arapType;
	}
	public void setOppPar(String oppPar) {
		this.oppPar = oppPar;
	}
	public void setMktCodeSipf(String mktCodeSipf) {
		this.mktCodeSipf = mktCodeSipf;
	}
	public void setArapBusiType(String arapBusiType) {
		this.arapBusiType = arapBusiType;
	}
	public void setCurTypeSipf(String curTypeSipf) {
		this.curTypeSipf = curTypeSipf;
	}
	public void setArapReason(String arapReason) {
		this.arapReason = arapReason;
	}
	public void setArapAmt(double arapAmt) {
		this.arapAmt = arapAmt;
	}
	public void setHarapDate(String harapDate) {
		this.harapDate = harapDate;
	}
	public void setArapDate(String arapDate) {
		this.arapDate = arapDate;
	}
	public void setSummary(String summary) {
		this.summary = summary;
	}
	public void setBusiDate(String busiDate) {
		this.busiDate = busiDate;
	}
	/**
	 * 转到查询页面
	 * @return
	 */
	public String initQueryAccrualInfo() {
		Calendar now = Calendar.getInstance();
		int year = now.get(Calendar.YEAR);
		int month = now.get(Calendar.MONTH ) + 1;
		int day = now.get(Calendar.DAY_OF_MONTH);
		StringBuffer date = new StringBuffer(Integer.toString(submitConfService.getNextTradeDate(day + month * 100 + year * 10000)));
		date.insert(6, "-").insert(4, "-");
		currentTradeDate = date.toString();
		now.set(Calendar.DAY_OF_MONTH, day + 1);
		year = now.get(Calendar.YEAR);
		month = now.get(Calendar.MONTH ) + 1;
		day = now.get(Calendar.DAY_OF_MONTH);
		date = new StringBuffer(Integer.toString(submitConfService.getNextTradeDate(day + month * 100 + year * 10000)));
		date.insert(6, "-").insert(4, "-");
		nextTradeDate = date.toString();
		arapTypeConfs = SysCodeDictLoader.getInstance().getArapTypeConfs();
		arapReasonConfs = SysCodeDictLoader.getInstance().getArapReasonConfs();
		oppParConfs = SysCodeDictLoader.getInstance().getOppParConfs();
		mktCodeSipfConfs = SysCodeDictLoader.getInstance().getMktCodeSipfConfs();
		arapBusiTypeConfs = SysCodeDictLoader.getInstance().getArapBusiTypeConfs();
		curTypeSipfConfs = SysCodeDictLoader.getInstance().getCurTypeSipfConfs();
		
		this.accrualManType = SysCodeDictLoader.getInstance().getSubmitConfValue("ACCRUAL_MAN_TYPE");
		return success();
	}
	
	/**
	 * 查询
	 * @return
	 */
	public String queryAccrualInfo() {
		if (busiDate == null || busiDate.trim().equals("")) {
			return error("参数不能为空");
		}
		QueryParameters qp = new QueryParameters();
		//设置分页信息
		qp.setPageNo(pageNo);
		qp.setPageSize(pageSize);
		qp.addParameter("busiDate", Integer.parseInt(busiDate.trim().replaceAll("-", "")));
		if (arapType != null && !arapType.trim().equals("")) {
			qp.addParameter("arapType", arapType);
		}
		try {
			accrualInfoPage = accrualInfoService.getAccrualInfoPage(qp);
			return success();
		} catch(Exception e) {
			e.printStackTrace();
			return error();
		}
	}
	
	/**
	 * 新增
	 * @return
	 */
	public String addAccrualInfo() {
		if (tradeDate == null || tradeDate.trim().equals("")
				|| arapType == null || arapType.trim().equals("")
				|| oppPar == null || oppPar.trim().equals("")
				|| mktCodeSipf == null || mktCodeSipf.trim().equals("")
				|| arapBusiType == null || arapBusiType.trim().equals("")
				|| curTypeSipf == null || curTypeSipf.trim().equals("")
				|| arapReason == null || arapReason.trim().equals("")
				|| harapDate == null || harapDate.trim().equals("")
//				|| arapDate == null || arapDate.trim().equals("")
				|| busiDate == null || busiDate.trim().equals("")) {
			return error("参数不能为空");
		}
		boolean success = false;
		try {
			Integer _arapDate = arapDate == null || arapDate.trim().equals("") ? null : Integer.valueOf(arapDate.trim().replaceAll("-", ""));
			accrualInfoService.addAccrualInfo(Integer.parseInt(tradeDate.trim().replaceAll("-", "")), 
					arapType, oppPar, mktCodeSipf, arapBusiType, curTypeSipf, arapReason, arapAmt, 
					Integer.parseInt(harapDate.trim().replaceAll("-", "")), 
					_arapDate, summary, 
					Integer.parseInt(busiDate.trim().replaceAll("-", "")));
			success = true;
			return success();
		} catch(ICISException e) {
			return error(e.getMessage());
		} catch(Exception e) {
			e.printStackTrace();
			return error();
		} finally {
			SessionParam user = (SessionParam)getSession().getAttribute(SysConstants.SESSION_USER_KEY);
			//记录系统日志
			EFRameInf.saveOpLog(user.getUSER_CODE(), EFRameInf.FUNCCODE_ACCRUAL, "增加证券公司资金应收应付汇总", success);
		}
	}
	
	/**
	 * 修改
	 * @return
	 */
	public String modifyAccrualInfo() {
		if (tradeDate == null || tradeDate.trim().equals("")
				|| arapType == null || arapType.trim().equals("")
				|| oppPar == null || oppPar.trim().equals("")
				|| mktCodeSipf == null || mktCodeSipf.trim().equals("")
				|| arapBusiType == null || arapBusiType.trim().equals("")
				|| curTypeSipf == null || curTypeSipf.trim().equals("")
				|| arapReason == null || arapReason.trim().equals("")
				|| harapDate == null || harapDate.trim().equals("")
//				|| arapDate == null || arapDate.trim().equals("")
				|| busiDate == null || busiDate.trim().equals("")) {
			return error("参数不能为空");
		}
		boolean success = false;
		try {
			Integer _arapDate = arapDate == null || arapDate.trim().equals("") ? null : Integer.valueOf(arapDate.trim().replaceAll("-", ""));
			accrualInfoService.modifyAccrualInfo(id,
					Integer.parseInt(tradeDate.trim().replaceAll("-", "")), arapType, 
					oppPar, mktCodeSipf, arapBusiType, curTypeSipf, arapReason, arapAmt, 
					Integer.parseInt(harapDate.trim().replaceAll("-", "")), 
					_arapDate, summary, 
					Integer.parseInt(busiDate.trim().replaceAll("-", "")));
			success = true;
			return success();
		} catch(ICISException e) {
			return error(e.getMessage());
		} catch(Exception e) {
			e.printStackTrace();
			return error();
		} finally {
			SessionParam user = (SessionParam)getSession().getAttribute(SysConstants.SESSION_USER_KEY);
			//记录系统日志
			EFRameInf.saveOpLog(user.getUSER_CODE(), EFRameInf.FUNCCODE_ACCRUAL, "修改证券公司资金应收应付汇总", success);
		}
	}
	
	/**
	 * 删除
	 * @return
	 */
	public String deleteAccrualInfo() {
		boolean success = false;
		try {
			accrualInfoService.deleteAccrualInfo(id);
			success = true;
			return success();
		} catch(Exception e) {
			e.printStackTrace();
			return error();
		} finally {
			SessionParam user = (SessionParam)getSession().getAttribute(SysConstants.SESSION_USER_KEY);
			//记录系统日志
			EFRameInf.saveOpLog(user.getUSER_CODE(), EFRameInf.FUNCCODE_ACCRUAL, "删除证券公司资金应收应付汇总", success);
		}
	}
}
